package com.smyh.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by Administrator on 2016/7/8.
 */
public class PointInfoDBHelper extends SQLiteOpenHelper{

    private final static String database_table_name = "point_info_table";
    /**
     * 主键id
     */
    private final static String point_id = "id";

    /**
     * 数据点类型：1-遥信，2-遥测，3-遥调，4-遥控
     */
    private final static String point_type = "pointType";

    /**
     * 点位值
     */
    private final static String point_value = "pointValue";
    /**
     * 组号
     */
    private final static String group_num = "groupNum";
    /**
     * 数据点-不同的点位值段：数据点不同
     */
    private final static String data_point = "dataPoint";

    public PointInfoDBHelper(Context context ) {
        super(context, SourceDB.db_name, null, SourceDB.db_version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE " + database_table_name + " (" + point_id
                + " INTEGER primary key autoincrement, " + point_type + " smallint , "+ point_value +" INTEGER," +
                group_num  + " smallint , " +  data_point  + " smallint );";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        String sql = "drop table if exists " + database_table_name +";";
        db.execSQL(sql);
        onCreate(db);
    }

    public Cursor select(){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cs = db.query(database_table_name,null,null,null,null,null,null);
        return  cs;
    }

    public long insert(){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(point_type,1);
        cv.put(point_value,2);
        long row = db.insert(database_table_name,null,cv);
        return row;
    }

    public void delete(long id){
        SQLiteDatabase db = this.getWritableDatabase();
        String where = point_id  + "=?";
        String[] whereValues = { Long.toString(id) };
        db.delete(database_table_name,where,whereValues);
    }

    public void delete(int id,String book_nameV,String book_authorV){
        SQLiteDatabase db = this.getWritableDatabase();
        String where = point_id  + "=?";
        String[] whereValues = { Long.toString(id) };

        ContentValues cv = new ContentValues();
        cv.put(group_num,book_nameV);
        cv.put(data_point,book_authorV);

        db.update(database_table_name,cv,where,whereValues);
    }

}
